package com.flyaudio.xiaopengautopilot.db;

import android.content.Context;
import android.os.Environment;
import android.os.SystemProperties;
import android.util.Log;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

import com.flyaudio.xiaopengautopilot.constant.JSONKeyName;
import com.flyaudio.xiaopengautopilot.entity.Consumption;
import com.flyaudio.xiaopengautopilot.entity.Sum;
import com.flyaudio.xiaopengautopilot.presenter.IVehicleModule;
import com.flyaudio.xiaopengautopilot.util.FileTool;
import com.flyaudio.xiaopengautopilot.util.JSONUtil;
import com.github.mikephil.charting.data.BarEntry;


import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;


@RunWith(AndroidJUnit4.class)
public class DBManagerTest {
    static final String TAG = "DBManagerTest";
    DBManager manager;
    SimpleDateFormat simpleDateFormat;
    Context appContext;
    @Before
    public void setUp() throws Exception {
        appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        manager = new DBManager(appContext,DBManager.DataBase_path);
        simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    }

    @After
    public void tearDown() throws Exception {

    }
    @Test
    public void resetAll(){
        manager.resetAVGTable();
        manager.resetSumTable();
        manager.resetConsumptionTable();
    }
    @Test
    public void resetSumTable() {
        manager.resetSumTable();
    }

    @Test
    public void resetConsumptionTable() {
        manager.resetConsumptionTable();
    }
    @Test
    public void insertConsumption() {
        for(int i= 0;i < 1000;++i){
            manager.insert(new Consumption("2016-06-01",String.valueOf(100+(float)i/10),String.valueOf(199+(float)i/1000),String.valueOf(i),i));
        }
    }
    @Test
    public void queryConsumptionSQLite() {

        Log.d(TAG,"queryConsumptionSQLite " + manager.queryAvgConsumptionLastOfNum(20));
        String temp = JSONUtil.getInstance().getMileageJSON(JSONKeyName.TotalMileage,"0").toString();
        int totalMileage = Integer.parseInt(temp);
        Log.d(TAG,"totalMileage  " + temp);
        {
            int setpMile = 20* 1000;
            int indexBegin = 100*1000 + setpMile;

            int indexEnd = 0;
            ArrayList<BarEntry> listEntry = new ArrayList<>();
            for (int i = 0; i < 6; ++i) {
                indexEnd = indexBegin - setpMile;
                Log.d("queryConsumptionSQLite 100", "indexEnd " + indexEnd + " indexBegin " + indexBegin);
                Log.d("queryConsumptionSQLite 100", "queryPowerSumInMileageRange " + manager.queryAvgPowerSumInMileageRange(totalMileage - indexBegin, totalMileage - indexEnd));
                indexBegin = indexEnd;
            }
        }
        {
            int setpMile = 2* 1000;
            int indexBegin = 10*1000 + setpMile;

            int indexEnd = 0;
            ArrayList<BarEntry> listEntry = new ArrayList<>();
            for (int i = 0; i < 6; ++i) {
                indexEnd = indexBegin - setpMile;
                Log.d("queryConsumptionSQLite 10", "indexEnd " + indexEnd + " indexBegin " + indexBegin);
                Log.d("queryConsumptionSQLite 10", "queryPowerSumInMileageRange " + manager.queryAvgPowerSumInMileageRange(totalMileage - indexBegin, totalMileage - indexEnd));
                indexBegin = indexEnd;
            }
        }
    }
    @Test
    public void insertSum() {

        String start="2016-06-01";
        String end="2017-06-30";
        Calendar st=Calendar.getInstance();
        Calendar ed=Calendar.getInstance();
        try {
            st.setTime(simpleDateFormat.parse(start));
            ed.setTime(simpleDateFormat.parse(end));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int i = 0;
        while(!st.after(ed)){
            String date = simpleDateFormat.format(st.getTime());
            st.add(Calendar.DAY_OF_YEAR,1);
            Log.d(TAG,"insert date " + date);
            Sum sum = new Sum();
            sum.setSumDate(date);
            sum.setSumMileage(Double.toString(i));
            sum.setSumTotalMileage(String.valueOf(i));
            sum.setSumPowerConsumption(String.valueOf(2.5));
            i++;
            manager.insert(sum);
        }

    }

    @Test
    public void querySumSQLite() {

        Log.d(TAG,"querySumSQLite " + manager.querySumDateMax());
        Log.d(TAG,"querySumSQLite " + manager.querySumDateMin());
        manager.deleteSumByDateBefore("2016-06-05");
        Log.d(TAG,"querySumSQLite " + manager.querySumInDate("2016-06-01","2016-06-08"));
        Log.d(TAG,"querySumSQLite  queryPowerConsumptionInDate " + manager.queryPowerConsumptionInDate("2016-06-01","2016-06-05"));
        String start="2016-06-05";
        String end="2017-06-30";
        Calendar st=Calendar.getInstance();
        Calendar ed=Calendar.getInstance();
        try {
            Date first = simpleDateFormat.parse(start);
            first.setDate(1);
            st.setTime(first);
            ed.setTime(simpleDateFormat.parse(end));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        st.add(Calendar.MONTH,1);
        Log.d(TAG,"MONTH  " + simpleDateFormat.format(st.getTime()));
    }
    @Test
    public void testPowerConsumption() {

        Log.d(TAG,"queryPowerConsumptionAvg "+ manager.queryPowerConsumptionAvg());
        Date date = new Date();
        String start="2016-06-01";
        String end="2017-06-30";
        String endDate = simpleDateFormat.format(date);
        date.setDate(1);
        String beginDate = simpleDateFormat.format(date);
        Log.d(TAG,"queryPowerConsumptionAvgInDate "+ manager.queryPowerConsumptionAvgInDate(start,end));

    }
    @Test
    public void  testcomputeMileage(){
//0.24528
//        Log.d(TAG, "testcomputeMileage: "+IVehicleModule.getInstance().computeMileage(1));
        File DestFile = new File(DBManager.DataBase_path);
        Log.d(TAG, "testcomputeMileage: "+ appContext.getDatabasePath(DBManager.DataBase_path));

    }

}